#include<stdio.h>
#include<sys/mman.h>
#include<sys/types.h>
#include<fcntl.h>
#include<unistd.h>
#include<string.h>
#include<stdlib.h>

#define RED    0x00FF0000//红色
#define PINK   0x00FF69B4//粉色
#define BLUE   0x000000FF//蓝色
#define YELLOW 0x00FFFF00//黄色

void draw_line(char *addr, int start_line, int color){
    for (int row = start_line; row < start_line+120; row++){
        int i = row % 480;
        for(int j = 0; j < 800; j++)
            memcpy(addr + i*800*4 + j*4, &color, 4);
    }
}

void scrolling_screen(char *addr){
    int line = 0;
    while (1){
        draw_line(addr, line,     RED);
        draw_line(addr, line+120, PINK);
        draw_line(addr, line+240, BLUE);
        draw_line(addr, line+360, YELLOW);
        line++;
        usleep(160);
        if(line >= 480)line = 0;
    }
}

int main(){
    int lcd = open("/dev/fb0", O_RDWR);
    if (lcd == -1){
        perror("open");
        return 0;
    }

    //内存映射
    char *addr = mmap(NULL, 800*480*4, PROT_READ|PROT_WRITE, MAP_SHARED, lcd, 0);
    if (addr == MAP_FAILED){
        perror("mmap");
        return 0;
    }

    //滚屏函数
    scrolling_screen(addr);

    //释放资源
    munmap(addr, 800*480*4);
    close(lcd);
    return 0;
}

// int main(int argc, char const *argv[])
// {
//     //打开lcd
//     int lcd = open("/dev/fb0", O_RDWR);
//     if (lcd == -1){
//         perror("open");
//         return 0;
//     }
//     //内存映射
//     char *addr = mmap(NULL, 800*480*4, PROT_READ|PROT_WRITE, MAP_SHARED, lcd, 0);
//     if (addr == MAP_FAILED){
//         perror("mmap");
//         return 0;
//     }
    
//     //滚屏函数

//     //释放资源
//     return 0;
// }
